package com.ffms.servlet; /**
 * @author 从卿
 * @create 2022/3/8-1:10
 */

import com.ffms.dao.impl.BankDaoImpl;
import com.ffms.entity.Bank;
import com.ffms.entity.FamilyMember;
import com.ffms.service.AdminService;
import com.ffms.service.impl.AdminServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/AddBankServlet")
public class AddBankServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        //1.接收视图层传来的表单数据并封装完成对象
        String bname = request.getParameter("bname");
        String bmoney = request.getParameter("bmoney");
        String mid = request.getParameter("mid");

        BankDaoImpl bankDao = new BankDaoImpl();
        Bank oldBank = bankDao.getByMId(mid);
        if (oldBank!=null){
            request.setAttribute("error", "已经为该用户添加过账户了");
            request.getRequestDispatcher("/PreAddBankServlet").forward(request, response);
            return;
        }
        Bank bank = new Bank(bname, bmoney,Integer.parseInt(mid));
        //2。 调用业务层完成添加操作
        AdminService adminService = new AdminServiceImpl();
        int n = adminService.addBank(bank);

        //根据结果跳转到不同的页面
        if (n > 0) {
//			成功跳转到全部成员页面
            response.sendRedirect(request.getContextPath() + "/AllBankServlet");
        } else {
//			失败则跳转到原页面
            request.setAttribute("error", "添加账户失败");
            request.getRequestDispatcher("/admin/bankAdd.jsp").forward(request, response);
        }
    }
}
